package splash.core.execution;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.net.ServerSocket;
import java.net.Socket;

public class FileThread implements Runnable{

        private File f;
        private ServerSocket s;
        private boolean read;

        public FileThread(File f, ServerSocket ss, boolean read)
        {
            this.f = f;
            this.s = ss;
            this.read = read;
        }
        
        private FileThread()
        {
        }

        public void run()
        {
            
            Socket sck = null;
            InputStream is = null;
            OutputStream os = null;
            
            try
            {
                s.setSoTimeout(20000);
                sck = s.accept();
                os = sck.getOutputStream();
                is = sck.getInputStream();
            }
            catch(IOException e)
            {
                e.printStackTrace();
                close();
                return;
            }
            
            /* File read/ directory listing */
            if(this.read)
            {
                /* File type */
                if(f.isDirectory())
                {
                    /* Send lines of data */
                    PrintWriter p = new PrintWriter(os, true);
                    File[] list = f.listFiles();

                    try
                    {
                        p.print(f.getCanonicalPath() + "\r\n");
                    }
                    catch(IOException e) {}
                    /* Prefix 'd' for directories */
                    for(File sf : list)
                    {
                        p.checkError();
                        p.print(((sf.isDirectory()) ? "d" : "f") + ":" +
                                sf.getName() + "\r\n");
                    }

                    p.print("END\r\n");

                }
                else
                {
                    /* Send file contents */
                    FileInputStream fis;
                    try
                    {
                        fis = new FileInputStream(f);
                    }
                    catch(FileNotFoundException fnfe)
                    {
                        return;
                    }

                    try
                    {
                        int bajt;
                        while((bajt = fis.read()) != -1)
                        {
                            os.write(bajt);
                        }
                    }
                    catch(IOException e)
                    {
                        return;
                    }

                }
            }
            /* File write */
            else
            {
                FileOutputStream fos;
                try
                {
                    fos = new FileOutputStream(f);
                    int bajt;
                    while((bajt = is.read()) != -1)
                        fos.write(bajt);
                }
                catch(IOException e)
                {
                    
                }
            }

            try
            {
                is.close();
                os.close();
                sck.close();
            }
            catch(Exception e)
            {
            }
        }

        private void close()
        {
            try
            {
                s.close();
            }
            catch(IOException ioe)
            {
                s = null;
                System.gc();
            }
        }
}
